<template>
  <el-container>
<!--    头部区域-->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="">
        <span>后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
<!--    主体-->
    <el-container>
<!--      左侧-->
      <el-aside :width="isFold?'':'200px'">
<!--        折叠按钮-->
        <div class="toggle-button" @click="toggleCollapse">|||</div>
<!--        侧边栏菜单区-->
        <el-menu
          :default-active="active"
          :router="true"
          :collapse="isFold"
          :collapse-transition="false"
          background-color="#333744"
          active-text-color="#409eff"
          text-color="#fff">
          <el-submenu :index="menuL1.id+''" v-for="menuL1 in menuList" :key="menuL1['id']">
            <template slot="title">
              <i :class="icons[menuL1.id]"></i>
              <span v-text="menuL1['authName']"/>
            </template>
            <el-menu-item :index="menuL2.path" @click="saveNavState(menuL2.path)" v-for="menuL2 in menuL1['children']" :key="menuL2['id']">
              <i class="el-icon-menu"></i>
              <span v-text="menuL2['authName']"/>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
<!--      右侧-->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'Home',
  data() {
    return {
      // 左侧菜单数据
      menuList: null,
      isFold: false,
      active: '',
      icons: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      }
    }
  },
  created() {
    // 一创建就区获取菜单
    this.getMenuList()
  },
  methods: {
    logout () {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    async getMenuList() {
      const res = await this.$http.get('menus')
      if (res.meta.status !== 200) {
        this.$msg.error(res.meta.msg)
        return false
      }
      this.menuList = res.data
      this.active = window.sessionStorage.getItem('activeMenu')
    },
    toggleCollapse() {

      // 菜单折叠展开
      this.isFold = !this.isFold
    },
    saveNavState(activeMenu) {
      // 菜单高亮保持,刷新页面依然有效
      window.sessionStorage.setItem('activeMenu', activeMenu)
      this.active = window.sessionStorage.getItem('activeMenu')
    }
  }
}
</script>

<style scoped>
  .el-header {
    display: flex;
    justify-content: space-between;
    background-color: #373d41;
    padding-left: 0;
    align-items: center;
    color: #ffffff;
    font-size: 20px;
  }
  .el-header div {
    display: flex;
    align-items: center;
  }
  .el-header div img {
    margin-right: 10px;
  }
  .el-aside {
    background-color: #333744;
  }
  .el-aside .el-menu{
    border: 0;
  }
  .el-main{
    background-color: #EAEDF1;
  }
  .el-container {
    height: 100%;
  }

  .toggle-button {
    background-color: #4A5064;
    color:#ffffff;
    font-size: 10px;
    line-height: 24px;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
</style>
